package operation.cust;
import zy.Cust;
import zy.Order;
import zy.Warehouse;

import java.util.HashMap;
import java.util.Scanner;

/*
 *@Student 张策
 */
public class cust2 implements Ioperation2 {
    @Override
    public void work(Warehouse warehouse, Cust cust) {
        Scanner scanner=new Scanner(System.in);
        warehouse.show();
        while (true){
        System.out.println("请输入要购买商品的名字");
        String name=scanner.nextLine();
        while (!warehouse.nameBool(name)){
            System.out.println("商品不存在，请重新输入");
            name=scanner.nextLine();
        }
        System.out.println("请输入要购买的数量:");
        String nu=scanner.nextLine();
        while (!warehouse.isNum(nu)){
            nu=scanner.nextLine();
        }
        int num=Integer.parseInt(nu);
        Order order = new Order(name, num, cust.getVIP());
        while (true){
        System.out.println("请问是否需要继续Y/N");
        String or=scanner.nextLine();
            while (!or.equals("Y")&&!or.equals("N")){
                System.out.println("请重新输入");
                or=scanner.nextLine();
            }
        if(or.equals("Y")){
            System.out.println("请输入要购买商品的名字");
            while (true){
             name=scanner.nextLine();
             if(warehouse.nameBool(name)){
                 break;
             }else {
                 System.out.println("不存在该商品，请重新输入");
             }
            }
             System.out.println("请输入要购买的数量:");
             nu=scanner.nextLine();
            while (!warehouse.isNum(nu)){
                nu=scanner.nextLine();
            }
             num=Integer.parseInt(nu);
            //刚才点过的菜品
             if(order.food.containsKey(name)){
                 order.food.put(name,order.food.get(name)+num);
                 order.setTotalPrice(num*warehouse.nameDish(name).getPrice());
             }else {//没点过的菜品
                 order.food.put(name,num);
                 order.setTotalPrice(num*warehouse.nameDish(name).getPrice());
             }
        }else if(or.equals("N")){
            System.out.println("下单成功，您的购物车如下");
            System.out.println("请稍等，正在付款中");
            if(order.totalPrice>cust.getMoney()){
                System.out.println("付款失败，余额不足！少爷冲钱后再购买吧！");
                return;
            }else {
            System.out.println(order);
            //扣钱
                cust.setMoney(cust.getMoney()-order.totalPrice);
                //将订单存入集合
                warehouse.addOrder(order);
                cust.consumptionRecords.add(order);
                return;
            }
            }
        }
        }
    }
}
